﻿using System.Collections.Generic;
using OMC.AddInHandling;
using OMC.Classification;
using Core = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Threading;

namespace OMC.DataManagement
{
    /// <summary>
    /// an object of this class could manage all the things about the
    /// data. such as MailData.
    /// </summary>
    /// <remarks>
    /// Copyright 2012 Stefan Lütolf
    /// </remarks>
    class Manager
    {
        private List<Folder> TheFolderList;
        private List<MenuItem> ThePredictedFolderNameList;
        private AddInHandler TheAddInHandler { get; set; }
        private ClassificationHandler TheClassificationHandler { get; set; }
        private MailContent TheSelectedMailItemContent;
        private Outlook.Accounts TheAccounts;
        private bool isDataSetLoaded = false;

        /// <summary>
        /// the constructor to create a object
        /// </summary>
        /// <param name="inAccounts">actual outlook accounts</param>
        /// <param name="inExplorer">actual outlook explorer</param>
        public Manager(Outlook.Accounts inAccounts, Outlook.Explorer inExplorer)
        {
            TheFolderList = new List<Folder>();
            TheAddInHandler = new AddInHandler(inExplorer, inAccounts, this);
            TheClassificationHandler = new ClassificationHandler();
            TheAccounts = inAccounts;
            ThePredictedFolderNameList = new List<MenuItem>();
        }

        /// <summary>
        /// to load the dataset as an extra thread
        /// </summary>
        public void load()
        {
            //load Dataset at first use
            Thread TheExtraThread = new Thread(new ThreadStart(loadDataSet));
            TheExtraThread.Start();
        }

        /// <summary>
        /// load the mail dataset by the first use of the AddIn
        /// and by refresh action
        /// </summary>
        private void loadDataSet()
        {
            lock (this)
            {
                isDataSetLoaded = false;

                if (!isDataSetLoaded)
                {
                    //Clear all
                    TheFolderList.Clear();
                    TheClassificationHandler.clear();

                    //load Mail Data
                    TheAddInHandler.loadMailData(ref TheFolderList);
                    TheClassificationHandler.createInstances(ref TheFolderList);

                    //Train Classifier
                    TheClassificationHandler.trainClassifier();

                    //Set up
                    isDataSetLoaded = true;
                    Monitor.PulseAll(this);
                }
            }
        }

        /// <summary>
        /// to save dataset to ARFF file
        /// </summary>
        /// <param name="inFilePath">the destination path inkl file name</param>
        /// <param name="inState">
        /// if inState is true, the arff file is filtered with the StringToWord filter
        /// if inState is false, the arff file isn't filtered, it's possible to read the content of it
        /// </param>
        public void saveToFile(ref string inFilePath,bool inState)
        {
            TheClassificationHandler.writeInstance(ref inFilePath, inState);
        }

        /// <summary>
        /// to handle the right click event of a mail item.
        /// </summary>
        /// <param name="inCommandBar">this Commandbar could be modified</param>
        /// <param name="inSelection">this Selection could be use to determine the Mailitem</param>
        public void handleItemContextMenuDisplay(Core.CommandBar inCommandBar, Outlook.Selection inSelection)
        {
            if (inSelection != null)
            {
                if (inSelection.Count == 1)
                {
                    try
                    {
                        object TheSelectedItem = inSelection[1];
                        if (TheSelectedItem is Outlook.MailItem)
                        {
                            Outlook.MailItem TheMailItem = TheSelectedItem as Outlook.MailItem;
                            if (TheMailItem != null)
                            {
                                //Create context popup menu
                                TheAddInHandler.creatContextMenuPopup(inCommandBar);

                                //load selected Maildata an create a MailContent object
                                TheSelectedMailItemContent = new MailContent(TheMailItem.Body,
                                                                             TheMailItem.Subject,
                                                                             TheMailItem.SenderEmailAddress,
                                                                             TheMailItem.To);

                                //wait while data set isn't loaded
                                while (!isDataSetLoaded) Monitor.Wait(this);

                                //clear Predicted Folder list
                                ThePredictedFolderNameList.Clear();

                                //Classify selected item
                                TheClassificationHandler.classify(ref TheSelectedMailItemContent, ref ThePredictedFolderNameList);
                                //String ClassifiedFolder = TheClassificationHandler.classify(TheSelectedMailItemContent);

                                //Create the Context Menu
                                TheAddInHandler.createContextMenuContent(inCommandBar, ref ThePredictedFolderNameList, TheMailItem);

                            }
                        }
                    }
                    catch
                    {

                    }

                }
            }
        }
    }
}